package com.rxt.thermopro.ui.fragment

import android.content.DialogInterface
import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.core.app.ShareCompat
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.app.common.base.BaseFragment
import com.rxt.thermopro.R
import com.rxt.thermopro.adapter.HistoryListAdapter
import com.rxt.thermopro.bean.DeviceBean
import com.rxt.thermopro.bean.ProbeHistoryBean
import com.rxt.thermopro.bean.ProbeHistoryWrapper
import com.rxt.thermopro.viewmodel.BleMessageViewModel
import com.rxt.thermopro.viewmodel.HistoryViewModel
import kotlinx.android.synthetic.main.fragment_history.*


class HistoryFragment : BaseFragment<HistoryViewModel>() {
    override fun layoutId(): Int {
        return R.layout.fragment_history
    }

    companion object {
        private const val ARG_POSITION = "currentPosition"
        private const val ARG_DEVICE_TYPE = "deviceType"
        private const val ARG_ADDRESS = "address"

        fun newInstance(
            position: Int,
            deviceType: Int, address: String
        ): HistoryFragment {
            return HistoryFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_POSITION, position)
                    putInt(ARG_DEVICE_TYPE, deviceType)
                    putString(ARG_ADDRESS, address)
                }
            }
        }
    }

    private lateinit var adapter: HistoryListAdapter
    private var currentPosition = -1
    private var mDeviceType = -1
    private var mAddress = ""

    override fun initView(savedInstanceState: Bundle?) {
        currentPosition = arguments?.getInt(ARG_POSITION, -1) ?: -1
        mDeviceType = arguments?.getInt(ARG_DEVICE_TYPE, -1) ?: -1
        mAddress = arguments?.getString(ARG_ADDRESS, "") ?: ""

        adapter = HistoryListAdapter(mutableListOf<ProbeHistoryWrapper>(),
            mDeviceType== BleMessageViewModel.DEVICE_TYPE_TP960)
        recyclerHistory.layoutManager = LinearLayoutManager(context)
        recyclerHistory.adapter = adapter

        mViewModel.historyList.observeInFragment(this, Observer {
            adapter.addAll(it)
        })
        initListener()
        mViewModel.getHistory(mAddress)
    }

    private fun initListener() {
        adapter.presenter=HistoryPresenter()

        backButton.setOnClickListener {
            requireActivity().onBackPressed()
        }

//        viewShare.setOnClickListener {
//            val uri = shareLayout.saveImage()
//            uri?.let {
//                val type = "image/*"
//
//                startActivity(
//                    ShareCompat
//                        .IntentBuilder
//                        .from(requireActivity())
//                        .setType(type)
//                        .setStream(Uri.parse(uri)).intent
//                )
//            }
//        }

    }

    inner class HistoryPresenter() {
        fun onItemClick(view: View, position: Int) {
            parentFragmentManager.beginTransaction()
                .setCustomAnimations(
                    R.anim.translate_into, R.anim.translate_out,
                    R.anim.translate_into_pop, R.anim.translate_out_exit
                )
                .add(
                    R.id.mainContent,
                    HistoryChartsFragment.newInstance(
                        adapter.list[position].itemList.last().historyIndex,
                        adapter.list[position].probeIndex, mAddress,mDeviceType
                    )
                )
                .addToBackStack("main")
                .commit()
        }

        fun deleteItem(item: ProbeHistoryWrapper, position: Int) {
            val dialog = AlertDialog.Builder(requireContext())
                .setMessage(R.string.text_sure_delete_history)
                .setNegativeButton(
                    R.string.text_Cancel
                ) { _, _ ->

                }
                .setPositiveButton(R.string.text_yes) { _, _ ->
                    adapter.remove(position)
                    mViewModel.removeHistory(item)
                }
                .show()
            dialog.getButton(DialogInterface.BUTTON_POSITIVE)
                .setTextColor(resources.getColor(R.color.gray_text))
            dialog.getButton(DialogInterface.BUTTON_NEGATIVE)
                .setTextColor(resources.getColor(R.color.gray_text))
        }
    }

}